package com.jack.vipmis.vip.sign.service;

import java.sql.Timestamp;

import org.springframework.stereotype.Service;

import com.jack.vipmis.common.service.BaseService;
import com.jack.vipmis.common.utils.PrettyTimeUtils;
import com.jack.vipmis.vip.sign.ScoreRuleAndMoveByte;
import com.jack.vipmis.vip.sign.entity.SignMsg;
import com.jack.vipmis.vip.sign.entity.VipSign;
import com.jack.vipmis.vip.sign.repository.VipSignRepository;

/**
 * 签到模块Service
 * 
 * @author JackLiang
 * @date 2014-6-22
 * @version 1.0
 */
@Service
public class VipSignService extends BaseService<VipSign, Long> {

	private static final String	SIGN_FAIL_MSG		= "签到失败！";
	private static final String	SIGN_SUCCESS_MSG	= "签到成功！";
	private static final String	SING_DOEN_MSG		= "今天已经签到了！";

	private VipSignRepository getVipSignRepository() {
		return (VipSignRepository) baseRepository;
	}

	/**
	 * 获取签到历史记录
	 * 
	 * @param userId
	 * @return
	 */
	public String signQuery(Long userId) {
		VipSign sign = getVipSignRepository().findByUserId(userId);
		return sign.getSignHistory().toString();
	}

	/**
	 * 点击签到，
	 * <p>
	 * 如果当天已经签过到了，提示已经签过到了；如果从来没有签过到，插入数据，把积分设置为1，连续签到次数设置为1，最后修改时间设置为当天， 历史为1
	 * ；如果今天没有签过到，首先计算出有多少天没签到了，如果是昨天签了的，连续签到次数加1，历史左移一位，积分按照积分规则加上；如果超过两天没有签到
	 * ，连续签到次数设置为1，历史左移天数位，积分加上签到单次的积分，时间为当前的修改时间；
	 * </p>
	 * 
	 * @param userId
	 * @return
	 */
	String todaySign(Long userId) {
		return null;
	}

	/**
	 * 领取礼包
	 * <p>
	 * 点击领取礼包，如果分数够，减去积分，允许该用户领取礼包；如果分数不过，提示积分不够
	 * </p>
	 * 
	 * @param userId
	 * @param score
	 * @return
	 */
	String getGiftPack(Long userId, Long score) {
		return null;
	}

	/**
	 * 
	 * @param userId
	 */
	public SignMsg signThenReturn(Long userId) {
		SignMsg message = new SignMsg();
		VipSign sign = getVipSignRepository().findByUserId(userId);
		if (sign != null) {
			Timestamp lastModifyTimeStamp = new Timestamp(sign.getLastModifyTime().getTime());
			Timestamp todayStartTimeStamp = PrettyTimeUtils.gettodayStartTimeStamp(); // 获取当日开始时间

			if (todayStartTimeStamp.after(lastModifyTimeStamp)) {
				final Long missDays = (System.currentTimeMillis() - sign.getLastModifyTime().getTime())
						/ (24 * 60 * 60 * 1000);

				Long newSignCount = sign.getSigncount();
				String newExt = "签到";
				if (missDays == 1L) { // 连续签到，加分，连续签到次数增加1 ，签到历史移动一位
					newSignCount += 1L;
				} else {// 不连续签到，加分，连续签到次数为1，签到历史移动missDays位
					newSignCount = 1L;
				}
				if (newSignCount >= 91) { // 签到超过90天，连续签到次数重置为1
					newSignCount = 1L;
					newExt = "连续签到天数重置为1，时间：" + PrettyTimeUtils.getDate(System.currentTimeMillis());
				}
				final long newSignHistory = ScoreRuleAndMoveByte.moveByte(sign.getSignHistory(), missDays);
				final Long newIntegration = sign.getIntegration() + ScoreRuleAndMoveByte.getScoreByRule(newSignCount);

				// 重新赋值
				sign.setSigncount(newSignCount);
				sign.setSignHistory(newSignHistory);
				sign.setIntegration(newIntegration);
				sign.setExt(newExt);
				// 更新当前记录
				update(sign);
			} else {
				// 返回提示今天已经签到了
				message.setMessage(SING_DOEN_MSG);
				message.setSignInfo(sign);
			}
		} else {
			VipSign vipSign = new VipSign();
			vipSign.setUserId(userId);
			vipSign.setSigncount(1L);
			vipSign.setIntegration(1L);
			vipSign.setSignHistory(1L);
			vipSign.setShow(true);
			VipSign returnSign = save(vipSign);
			if (returnSign != null) {
				message.setMessage(SIGN_SUCCESS_MSG);
				message.setSignInfo(returnSign);
			} else {
				message.setMessage(SIGN_FAIL_MSG);
			}
		}
		return message;
	}
}
